package f.app.al;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class Database {
	public static final String KEY_ROWID = "_id";
	public static final String KEY_NAME = "";
	public static final String KEY_CATEGORY = "";
	public static final String KEY_COST = "";
	private static final String TAG = "DBAdapter";
	private static final String DATABASE_NAME = "expenses";
	private static final String TABLE_NAME = "budget_entry";
	private static final String INCOME_TABLE_NAME = "income_budget_entry";
	private static final int DATABASE_VERSION = 1;
	private final Context context;
	private DatabaseHelper DBHelper;

	public Database(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}

	public void addEntry(Entry transEntry) {

		String addCommand = "INSERT INTO "
				+ TABLE_NAME
				+ "(name, category, amount, transactionYear, transactionMonth, transactionDay) VALUES('"
				+ transEntry.name + "', '" + transEntry.category + "', '"
				+ transEntry.amount + "', '" + transEntry.year + "', '"
				+ transEntry.month + "', '" + transEntry.day + "');";
		DBHelper.getWritableDatabase().execSQL(addCommand);
		DBHelper.close();
	}
	public void addIncomeEntry(int food, int transport, int cloths, int hobby){
		
		String addIncomeCommand = "INSERT INTO "
				+ INCOME_TABLE_NAME
				+ "(food, transport, cloths, hobby) VALUES('"
				+ food + "', '" + transport + "', '"
				+ cloths + "', '" + hobby + "');";
		DBHelper.getWritableDatabase().execSQL(addIncomeCommand);
		DBHelper.close();
		
	}
	public Income getIncomeEntry(){
		String sumIncomeQuery = "SELECT * FROM " + INCOME_TABLE_NAME;
		Cursor cursor = DBHelper.getReadableDatabase().rawQuery(sumIncomeQuery,null);
		cursor.moveToFirst();
		DBHelper.close();
		Income incomeObject = cursorToIncome(cursor);
		return incomeObject;
	}

	public int getCategorySum(Integer category) {
		String sumQuery = "SELECT SUM(amount) FROM " + TABLE_NAME
				+ " WHERE category='" + category + "'";
		Cursor cursor = DBHelper.getReadableDatabase().rawQuery(sumQuery, null);
		cursor.moveToFirst();
		DBHelper.close();
		return cursor.getInt(0);
	}

	public int getCategoryPercentage(Integer category) {
		String percentageQuery = "SELECT (SELECT SUM(amount) FROM "
				+ TABLE_NAME + " WHERE category='" + category
				+ "')/(SELECT SUM(amount) FROM " + TABLE_NAME + ") * 100;";
		Cursor cursor = DBHelper.getReadableDatabase().rawQuery(
				percentageQuery, null);
		cursor.moveToFirst();
		DBHelper.close();
		return cursor.getInt(0);
	}

	public List<Entry> getAllEntries() {
		List<Entry> entries = new ArrayList<Entry>();
		String allQuery = "SELECT * FROM " + TABLE_NAME + ";";
		Cursor cursor = DBHelper.getReadableDatabase().rawQuery(allQuery, null);
		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			Entry entry = cursorToEntry(cursor);
			entries.add(entry);
			cursor.moveToNext();
		}
		cursor.close();
		DBHelper.close();
		return entries;
	}
	private Income cursorToIncome (Cursor cursor){
		Income income = new Income();
		income.food = cursor.getInt(0);
		income.transport = cursor.getInt(1);
		income.cloths = cursor.getInt(2);
		income.hobby = cursor.getInt(3);
		return income;
		
	}
	private Entry cursorToEntry(Cursor cursor) {
		Entry entry = new Entry();
		entry.name = cursor.getString(1);
		entry.category = cursor.getInt(2);
		entry.amount = cursor.getInt(3);
		entry.year = cursor.getInt(4);
		entry.month = cursor.getInt(5);
		entry.day = cursor.getInt(6);
		return entry;
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			
			createTable(db);

		}

		private void createTable(SQLiteDatabase db) {
			String createIncomeCommand = "create table "
					+ INCOME_TABLE_NAME
					+ "("
					+ "food INTEGER NOT NULL,transport INTEGER NOT NULL,cloths INTEGER NOT NULL,hobby INTEGER NOT NULL); ";
			String createCommand = "create table "
					+ TABLE_NAME
					+ " ("
					+ "_id integer primary key autoincrement, "
					+ "name TEXT NOT NULL, category INTEGER NOT NULL, amount INTEGER NOT NULL, "
					+ "transactionYear INTEGER NOT NULL, transactionMonth INTEGER NOT NULL,transactionDay INTEGER NOT NULL);";
			db.execSQL(createCommand);
			db.execSQL(createIncomeCommand);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS expenses;");
			createTable(db);
		}

	}
}
